/*
 * @Author: 胡海
 * @Date: 2020-02-21 18:23:26
 * @LastEditors: 胡海
 * @LastEditTime: 2020-03-07 22:27:44
 * @Description: 
 */
const {
    sequelize
} = require('../../core/db.js')


const {
    Sequelize,
    Model
} = require('sequelize')

const {
    BtnRoles
} = require('./btnRoles')

// define
class UserRoles extends Model {
    // 业务表
    static async addRoles(ctx, v) { // 新增角色
        let request = {
            rolesName: v.get('body.rolesName'),
            weights: v.get('body.weights'),
            rolesType: v.get('body.rolesType'),
        }
        const roles = await UserRoles.findOne({
            where: {
                rolesType: request.rolesType
            }
        })
        if (roles) {
            throw new global.errs.LikeError('角色已存在')
        }
        await UserRoles.create(request)
        throw new global.errs.Success()
    }

    static async upRoles(ctx, v) { // 修改角色
        let request = {
            rolesName: v.get('body.rolesName'),
            weights: v.get('body.weights'),
            rolesType: v.get('body.rolesType'),
        }
        const roles = await UserRoles.update(request, {
            where: {
                id: v.get('body.id'),
            }
        })
        throw new global.errs.Success()
    }

    static async getRolesList(ctx, v) { // 角色列表
        let request = {
            start: v.get('body.start'),
            count: v.get('body.count'),
        }
        const total = await UserRoles.count()
        const list = await UserRoles.findAll({
            limit: request.count,
            offset: (request.start - 1) * request.count,
        })
        let data = {
            total,
            list
        }
        return data
    }
    static async RolesBtnList(ctx, v) { // 角色按钮
        let request = {
            rolesType: v.get('body.rolesType'),
            menuName: v.get('body.menuName'),
        }
        let data = null
        if (request.menuName) {
            data = await BtnRoles.findAll({
                where: request
            })
        } else {
            data = await BtnRoles.findAll({
                where: {
                    rolesType: v.get('body.rolesType'),
                }
            })
        }

        return data
    }
    static async setRolesMenu(ctx, v) { // 角色菜单
        let request = {
            menu: v.get('body.menu'),
            rolesType: v.get('body.rolesType'),
        }
        let menu = JSON.stringify(JSON.parse(request.menu).filter(item => item.indexOf('Btn_') === -1))
        let btn = JSON.parse(request.menu).filter(item => item.indexOf('Btn_') !== -1)
        const p = await UserRoles.update({
            menu
        }, {
            where: {
                rolesType: request.rolesType
            }
        })
        if (btn) {
            for (let i = 0; i < btn.length; i++) {
                let re = await BtnRoles.findOne({
                    where: {
                        btnName: btn[i]
                    }
                })
                if (re) {

                    await BtnRoles.update({
                        btnName: btn[i],
                        menuName: btn[i].match(/_(\S*)_/)[1],
                        rolesType: request.rolesType
                    }, {
                        where: {
                            btnName: btn[i]
                        }
                    })
                } else {
                    await BtnRoles.create({
                        btnName: btn[i],
                        menuName: btn[i].match(/_(\S*)_/)[1],
                        rolesType: request.rolesType
                    })
                }
            }
        }
        throw new global.errs.Success()
    }
}

UserRoles.init({
    rolesName: Sequelize.STRING,
    menu: Sequelize.STRING,
    weights: Sequelize.INTEGER,
    rolesType: Sequelize.INTEGER,
}, {
    sequelize,
    tableName: 'userRoles',
    alter: true
})

module.exports = {
    UserRoles
}

// 数据迁移 SQL 更新 风险